package com.changgou.goods.service.impl;


import com.changgou.goods.dao.BrandMapper;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import com.github.pagehelper.Page;
import java.util.List;
import java.util.Map;

/**
 * @author shkstart
 * @create 2020-08-22 0:43
 */
@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    /*
    * 品牌列表查询
    * */
    @Override
    public List<Brand> findAll() {
        List<Brand> brandList = brandMapper.selectAll();
        return brandList;
    }

    /*
    根据id查询品牌
    * */
    @Override
    public Brand findById(Integer id) {
        Brand brand = brandMapper.selectByPrimaryKey(id);

        return brand;
    }
/*
* 新增品牌
* */
    @Override
    public void addBrand(Brand brand) {
        brandMapper.insertSelective(brand);
    }
/*
* 修改品牌
* */
    @Override
    public void updateBrand(Brand brand) {
        brandMapper.updateByPrimaryKeySelective(brand);

    }
/*
* 删除品牌
* */
    @Override
    public void delete(Integer id) {
      brandMapper.deleteByPrimaryKey(id);
    }

    /*
    * 品牌列表的条件查询
    * */
    @Override
    public List<Brand> findList(Map<String, Object> searchMap) {
        Example example = new Example(Brand.class);
        //封装查询条件
        Example.Criteria criteria = example.createCriteria();

        if(searchMap != null){
            //品牌名称（模糊）like %
            if(searchMap.get("name") != null && !"".equals(searchMap.get("name"))){
                    criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            //按照品牌首字母进行查询（精确）
            if(searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }

        }
        List<Brand> brandList = brandMapper.selectByExample(example);
        return brandList;
    }
    /**
     * 品牌列表分页查询
     * page:当前的页码
     * size:每页显示多少条
     */
    @Override
    public Page<Brand> findPage(int page, int size) {
        PageHelper.startPage(page,size);
        Page<Brand> page1 = (Page<Brand>) brandMapper.selectAll();
        return page1;
    }
    /**
     * 品牌列表分页+条件查询
     */
    @Override
    public Page<Brand> findPage(Map<String, Object> searchMap, int page, int size) {
        //设置分页
        PageHelper.startPage(page,size);

        //设置查询条件
        Example example = new Example(Brand.class);

        Example.Criteria criteria = example.createCriteria();
        if(searchMap != null){
            //设置品牌明名称模糊查询
            if(searchMap.get("name") != null && !"".equals(searchMap.get("name"))){
                criteria.andLike("name","%"+searchMap.get("name")+"%");
            }

            //设置品牌首字母的精确查询
            if(searchMap.get("letter") != null && !"".equals(searchMap.get("letter"))){
                criteria.andEqualTo("letter",searchMap.get("letter"));
            }
        }

       Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);

        return pageInfo;
    }
}
